#pragma once
#include<string>
#include<vector>
using namespace std;
namespace celia
{
	template<class K>
	struct HashFunc
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};
	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& key)
		{
			size_t sum = 0;
			for (int i = 0; i < key.size(); i++)
			{
				sum += (size_t)key[i];
				sum *= 131;
			}
			return sum;
		}
	};
	inline unsigned long __stl_next_prime(unsigned long n)
	{
		// Note: assumes long is at least 32 bits.
		static const int __stl_num_primes = 28;
		static const unsigned long __stl_prime_list[__stl_num_primes] = {
			53, 97, 193, 389, 769,
			1543, 3079, 6151, 12289, 24593,
			49157, 98317, 196613, 393241, 786433,
			1572869, 3145739, 6291469, 12582917, 25165843,
			50331653, 100663319, 201326611, 402653189, 805306457,
			1610612741, 3221225473, 4294967291
		};
		const unsigned long* first = __stl_prime_list;
		const unsigned long* last = __stl_prime_list + __stl_num_primes;
		const unsigned long* pos = lower_bound(first, last, n);
		return pos == last ? *(last - 1) : *pos;
	}
	template<class T>
	struct HashNode
	{
		HashNode(const T& data)
			:_data(data)
			,_next(nullptr)
		{}
		T _data;
		HashNode<T>* _next;
	};
	template<class K, class T, class KeyOfT, class Hash = HashFunc<K>>
	class HashTable
	{
		typedef HashNode<T> Node;
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend class HashTableIterator;
	public:
		typedef HashTableIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;
		typedef HashTableIterator<K, T, const T&, const T*, KeyOfT, Hash> Const_Iterator;
		HashTable()
			:_table(__stl_next_prime(0))
			,_n(0)
		{}
		~HashTable()
		{
			for (int i = 0; i < _table.size(); i++)
			{
				if (_table[i])
				{
					Node* cur = _table[i];
					while (cur)
					{
						Node* next = cur->_next;
						delete cur;
						cur = next;
					}
					_table[i] = nullptr;
				}
			}
		}
		Const_Iterator Begin() const
		{
			for (int i = 0; i < _table.size(); i++)
			{
				if (_table[i])
					return Const_Iterator(_table[i], this);
			}
			return Const_Iterator(nullptr, this);
		}
		Const_Iterator End() const
		{
			return Const_Iterator(nullptr, this);
		}
		Iterator Begin()
		{
			for (int i = 0; i < _table.size(); i++)
			{
				if (_table[i])
					return Iterator(_table[i], this);
			}
			return Iterator(nullptr, this);
		}
		Iterator End()
		{
			return Iterator(nullptr, this);
		}
		Iterator Find(const K& data)
		{
			Hash hash;
			KeyOfT kot;
			size_t hash0 = hash(data) % _table.size();
			Node* cur = _table[hash0];
			while (cur)
			{
				if (kot(cur->_data) == data)
				{
					return {cur, this};
				}
				cur = cur->_next;
			}
			return {nullptr, this};
		}
		pair<Iterator, bool> Insert(const T& data)
		{
			Hash hash;
			KeyOfT kot;
			Iterator it = Find(kot(data));
			if (it != End())
				return { it, false };
			if (_n / _table.size() == 1)
			{
				vector<Node*> new_table(__stl_next_prime(_n + 1));
				for (int i = 0; i < _table.size(); i++)
				{
					if (_table[i])
					{
						Node* cur = _table[i];
						while (cur)
						{
							Node* next = cur->_next;
							size_t hash0 = hash(kot(cur->_data)) % new_table.size();
							cur->_next = new_table[hash0];
							new_table[hash0] = cur;
							cur = next;
						}
						_table[i] = nullptr;
					}
				}
				_table.swap(new_table);
			}
			size_t hash0 = hash(kot(data)) % _table.size();
			Node* newnode = new Node(data);
			newnode->_next = _table[hash0];
			_table[hash0] = newnode;
			_n++;
			return { {newnode, this}, true };
		}
		bool Erase(const K& data)
		{
			Hash hash;
			if (!Find(data))
				return false;
			size_t hash0 = hash(data) % _table.size();
			Node* cur = _table[hash0];
			if (hash(cur->_data) == hash(data))
			{
				Node* del = cur;
				_table[hash0] = cur->_next;
				delete del;
			}
			else
			{
				while (hash(cur->_next->_data) == hash(data))
				{
					cur = cur->_next;
				}
				Node* del = cur->_next;
				cur->_next = del->_next;
				delete del;
			}
			_n--;
			return true;
		}
		size_t Size()
		{
			return _n;
		}
	private:
		vector<Node*> _table;
		size_t _n;
	};
	template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
	class HashTableIterator
	{
		typedef HashNode<T> Node;
		typedef HashTable<K, T, KeyOfT, Hash> Table;
		typedef HashTableIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;
	public:
		HashTableIterator(Node* node, const Table* table)
			:_node(node)
			, _table_node(table)
		{}


		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		Self operator++()
		{
			Hash hash;
			KeyOfT kot;
			if (_node->_next)
			{
				_node = _node->_next;
			}
			else
			{
				size_t hash0 = hash(kot(_node->_data)) % _table_node->_table.size();
				hash0++;
				while (hash0 < _table_node->_table.size())
				{
					_node = _table_node->_table[hash0];
					if (_node)
						break;
					hash0++;
				}
				if (hash0 == _table_node->_table.size())
					_node = nullptr;
			}
			return *this;
		}
		bool operator!=(const Self& self)
		{
			return _node != self._node;
		}
		bool operator==(const Self& self)
		{
			return _node == self._node;
		}
	private:
		Node* _node;
		const Table* _table_node;
	};
}